package com.mercury.user.service.impl;

import com.mercury.user.entity.CenterMall;
import com.mercury.user.entity.CenterMallUser;
import com.mercury.user.entity.CenterUser;
import com.mercury.user.mapper.CenterMallMapper;
import com.mercury.user.service.CenterMallService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mercury.user.service.CenterMallUserService;
import com.mercury.user.service.CenterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tong
 * @since 2021-11-22
 */
@Service
public class CenterMallServiceImpl extends ServiceImpl<CenterMallMapper, CenterMall> implements CenterMallService {

    @Autowired
    private CenterUserService userService;

    @Autowired
    private CenterMallUserService mallUserService;

    @Override
    public Boolean exchangeItem(String useId, int itemId) {
        CenterUser user = userService.getById(useId);
        int coffeeBean = user.getCoffeeBean();
        CenterMall item = baseMapper.selectById(itemId);
        if(coffeeBean < item.getMallBeans()) return false;
        CenterMallUser centerMallUser = new CenterMallUser();
        centerMallUser.setUserId(useId);
        centerMallUser.setGoodsId(itemId);
        centerMallUser.setDeleted(false);
        mallUserService.save(centerMallUser);
        user.setCoffeeBean(coffeeBean-item.getMallBeans());
        return userService.updateById(user);
    }
}
